package cn.bjit.controller;


import cn.bjit.enums.BizCodeEnum;
import cn.bjit.exception.BizException;
import cn.bjit.model.AddressDO;
import cn.bjit.request.AddressAddRequest;
import cn.bjit.service.AddressService;
import cn.bjit.util.JsonData;
import cn.bjit.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author yx
 * @since 2022-05-31
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @ApiOperation("新增收货地址")
    @PostMapping("add")
    public JsonData add(@ApiParam("地址对象") @RequestBody AddressAddRequest addressAddRequest) {
        addressService.add(addressAddRequest);
        return JsonData.buildSuccess();
    }

    //http://localhost:9001/api/address/v1/find/39
    @ApiOperation("根据id查询地址详情")
    @GetMapping("find/{address_id}")
    public Object findById(@ApiParam(value = "地址id", required = true) @PathVariable("address_id") long id) {
        AddressVO addressVO = addressService.findById(id);

        return addressVO == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS) : JsonData.buildSuccess(addressVO);
    }

    @ApiOperation("根据id删除地址")
    @GetMapping("del/{address_id}")
    public JsonData del(@ApiParam("地址id") @PathVariable("address_id") long address_id) {
        int row = addressService.del(address_id);
        return row == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }

    @ApiOperation("查询所有的地址")
    @GetMapping("list")
    public JsonData findUserAllAddress() {
        List<AddressVO> addressVOList = addressService.list();
        return JsonData.buildSuccess(addressVOList);
    }

}

